package com.lahuan.config.datasouce;

import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.*;
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
import com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement;
import com.alibaba.fastjson.JSON;
import com.lahuan.config.datasouce.entity.*;

import java.sql.SQLSyntaxErrorException;
import java.util.ArrayList;
import java.util.List;

public class SqlParserUtil {
    public static void main(String[] args) {
        System.out.println(String.class.getName());
    }


    public static CustomSqlParserResult parseResult(String sql) {
        try {
            String dbType = "mysql";
            SQLStatement statement = parser(sql, dbType);
            if (statement instanceof MySqlInsertStatement) {
                return parseInsert((MySqlInsertStatement) statement);
            }
            if (statement instanceof MySqlUpdateStatement) {
                return parseUpdate((MySqlUpdateStatement) statement);
            }
            if (statement instanceof MySqlDeleteStatement) {
                return parseDelete((MySqlDeleteStatement) statement);
            }
        }catch (Exception ex){
            System.out.println("sql解析出现异常");
            ex.printStackTrace();
        }
        //这里只做简单的单条语句
        return null;
    }
    private static SQLStatement parser(String sql, String dbType) {
        List<SQLStatement> list = SQLUtils.parseStatements(sql, dbType);
        if (list.size() > 1) {
            //这里只做简单的单条语句
            return null;
        }
        return list.get(0);
    }
    private static InsertCustomSqlParserResult parseInsert(MySqlInsertStatement insertStatement) {
        InsertCustomSqlParserResult result = new InsertCustomSqlParserResult();
        SQLIdentifierExpr tableName = (SQLIdentifierExpr) insertStatement.getTableName();
        List<SQLExpr> columns = insertStatement.getColumns();
        SQLInsertStatement.ValuesClause values = insertStatement.getValues();
        List<SQLExpr> valueList = values.getValues();
        List<ColumnValuePair> insertColumnValue = new ArrayList<>();
        for (int i = 0; i < columns.size(); i++) {
            SQLIdentifierExpr column = (SQLIdentifierExpr) columns.get(i);
            SQLValuableExpr valuableExpr = (SQLValuableExpr) valueList.get(i);
            ColumnValuePair pair = new ColumnValuePair();
            pair.setColumn(column.getName());
            pair.setValue(valuableExpr.getValue());
            insertColumnValue.add(pair);
        }
        result.setTableName(tableName.getName());
        result.setInsertColumnValue(insertColumnValue);
        //ColumnValuePair idColumnValue;
        return result;

    }

    private static UpdateCustomSqlParserResult parseUpdate(MySqlUpdateStatement updateStatement)  {
        UpdateCustomSqlParserResult result = new UpdateCustomSqlParserResult();
        //
        List<ColumnValuePair> updateColumnValue = new ArrayList<>();
        List<SQLUpdateSetItem> updateItems = updateStatement.getItems();
        for (SQLUpdateSetItem child : updateItems) {
            SQLIdentifierExpr column = (SQLIdentifierExpr) child.getColumn();
            SQLValuableExpr value = (SQLValuableExpr) child.getValue();
            ColumnValuePair pair = new ColumnValuePair();
            pair.setValue(value.getValue());
            pair.setColumn(column.getName());
            updateColumnValue.add(pair);
        }
        List<SQLObject> list = updateStatement.getWhere().getChildren();
        SQLIdentifierExpr idColumn = (SQLIdentifierExpr) list.get(0);
        SQLIdentifierExpr tableName = (SQLIdentifierExpr)updateStatement.getTableName();
        SQLValuableExpr idExpr = (SQLValuableExpr) list.get(1);
        ColumnValuePair idColumnValue = new ColumnValuePair();
        idColumnValue.setColumn(idColumn.getName());
        idColumnValue.setValue(idExpr.getValue());

        result.setTableName(tableName.getName());
        result.setUpdateColumnValue(updateColumnValue);
        result.setIdColumnValue(idColumnValue);
        //result.setBeforeColumnValue();
        return result;

    }

    private static DeleteCustomSqlParserResult parseDelete(  MySqlDeleteStatement deleteStatement)  {
        DeleteCustomSqlParserResult result = new DeleteCustomSqlParserResult();
        SQLIdentifierExpr tableName = (SQLIdentifierExpr) deleteStatement.getTableName();
        List<SQLObject> list = deleteStatement.getWhere().getChildren();
        SQLIdentifierExpr idColumn = (SQLIdentifierExpr) list.get(0);
        SQLValuableExpr idExpr = (SQLValuableExpr) list.get(1);
        ColumnValuePair idColumnValue = new ColumnValuePair();
        idColumnValue.setColumn(idColumn.getName());
        idColumnValue.setValue(idExpr.getValue());
        result.setTableName(tableName.getName());
        result.setIdColumnValue(idColumnValue);
        //result.setBeforeColumnValue();
        return result;
    }


}
